//
//  BeijingViewController.m
//  littleLight's Project-A
//
//  Created by dllo on 16/5/25.
//  Copyright © 2016年 light. All rights reserved.
//

#import "BeijingViewController.h"
#import <AFNetworking.h>
#import "ModulesModel.h"
#import "ModulesTableViewCell.h"
#import "Hunter_listModel.h"
#import "HunterTableViewCell.h"
#import "SpotsModel.h"
#import "SpotsTableViewCell.h"
@interface BeijingViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *cheapArray;
@property (nonatomic, strong) NSMutableArray *lastArray;
@property (nonatomic, strong) NSMutableArray *bestArray;
@property (nonatomic, strong) NSMutableArray *playArray;
@property (nonatomic, strong) NSMutableArray *hunterArray;
@property (nonatomic, strong) NSMutableArray *spotsArray;
//刷新
//记录下一页
@property (nonatomic, assign) NSInteger nextPage;
//判断上拉下拉(yes是上拉)
@property (nonatomic, assign) BOOL isUpLoading;
@end

@implementation BeijingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.cheapArray = [NSMutableArray array];
    self.lastArray = [NSMutableArray array];
    self.bestArray = [NSMutableArray array];
    self.playArray = [NSMutableArray array];
    self.hunterArray = [NSMutableArray array];
    self.spotsArray = [NSMutableArray array];
    self.navigationItem.title = @"热门活动";
    //字体设为白色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName: [UIFont systemFontOfSize:22],NSFontAttributeName: [UIFont boldSystemFontOfSize:22]}];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.showsHorizontalScrollIndicator = NO;
    self.myTableView.showsVerticalScrollIndicator = NO;
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView registerClass:[ModulesTableViewCell class] forCellReuseIdentifier:@"cheapReuse"];
    [self.myTableView registerClass:[ModulesTableViewCell class] forCellReuseIdentifier:@"lastReuse"];
    [self.myTableView registerClass:[ModulesTableViewCell class] forCellReuseIdentifier:@"bestReuse"];
    [self.myTableView registerClass:[ModulesTableViewCell class] forCellReuseIdentifier:@"playReuse"];
    [self.myTableView registerClass:[HunterTableViewCell class] forCellReuseIdentifier:@"hunterReuse"];
    [self.myTableView registerClass:[SpotsTableViewCell class] forCellReuseIdentifier:@"spotsReuse"];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    self.isUpLoading = NO;
  //  [self addFooter];
    [self addHeader];
    
    
    
    
}

- (void)leftAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)getDataList:(NSInteger)nextPage
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    
    [manager GET:@"http://api.breadtrip.com/hunter/products/newstyle/?city_name=%E5%8C%97%E4%BA%AC&lat=38.88258356205654&lng=121.53924515009305" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // NSLog(@"*************%@",responseObject);
        NSDictionary *dataDic = [responseObject objectForKey:@"data"];
        NSDictionary *spotsDic = [dataDic objectForKey:@"spots"];
        NSMutableArray *spotListArr = [spotsDic objectForKey:@"spot_list"];
        if (self.isUpLoading == NO) {
            [self.spotsArray removeAllObjects];
            [self.hunterArray removeAllObjects];
            [self.cheapArray removeAllObjects];
            [self.lastArray removeAllObjects];
            [self.bestArray removeAllObjects];
            [self.playArray removeAllObjects];
            
        }
        
        
        
        for (NSDictionary *dic in spotListArr) {
            SpotsModel *model = [[SpotsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.spotsArray addObject:model];
         //   NSLog(@"************%@",model.name);
        }
        
        
        
        
        NSDictionary *huntersDic = [dataDic objectForKey:@"hunters"];
        NSMutableArray *huntersArray = [huntersDic objectForKey:@"hunter_list"];
        for (NSDictionary *dic in huntersArray) {
            Hunter_listModel *model = [[Hunter_listModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.hunterArray addObject:model];
            //  NSLog(@"*********%@",model.name);
        }
        
        
        
        
        
        NSMutableArray *modulesArr = [dataDic objectForKey:@"product_modules"];
        NSDictionary *cheapDic = [modulesArr objectAtIndex:0];
        NSMutableArray *cheapArr = [cheapDic objectForKey:@"product_list"];
        for (NSDictionary *dic in cheapArr) {
            ModulesModel *model = [[ModulesModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.cheapArray addObject:model];
            // NSLog(@"************%@",self.cheapArray);
        }
        NSDictionary *lastDic = [modulesArr objectAtIndex:1];
        NSMutableArray *lastArr = [lastDic objectForKey:@"product_list"];
        for (NSDictionary *dic in lastArr) {
            ModulesModel *model = [[ModulesModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.lastArray addObject:model];
        }
        NSDictionary *bestDic = [modulesArr objectAtIndex:2];
        NSMutableArray *bestArr = [bestDic objectForKey:@"product_list"];
        for (NSDictionary *dic in bestArr) {
            ModulesModel *model = [[ModulesModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.bestArray addObject:model];
            
        }
        NSDictionary *playDic = [modulesArr lastObject];
        NSMutableArray *playArr = [playDic objectForKey:@"product_list"];
        for (NSDictionary *dic in playArr) {
            ModulesModel *model = [[ModulesModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.playArray addObject:model];
        }
        
        [self.myTableView footerEndRefreshing];
        [self.myTableView headerEndRefreshing];
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.myTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
        
    }];
    
}


#pragma mark --下拉刷新
-(void)addHeader{
    //block中不能使用self,会报错
    __block BeijingViewController *recommendVC = self;
    
    [self.myTableView addHeaderWithCallback:^{
        //记录页码
        recommendVC.nextPage = 1;
        //标记下拉刷新
        recommendVC.isUpLoading = NO;
        //请求数据
        [recommendVC getDataList:recommendVC.nextPage];
        
    }];
    
    
    //进入程序就开始下拉刷新
    [self.myTableView headerBeginRefreshing];
}

#pragma mark -- 上拉加载更多
//-(void)addFooter{
//    
//    __block BeijingViewController *recommendVC = self;
//    //添加上拉刷新尾部控件
//    [self.myTableView addFooterWithCallback:^{
//        
//        recommendVC.nextPage ++;
//        //标记为上拉操作
//        recommendVC.isUpLoading = YES;
//        //请求数据
//        [recommendVC getDataList:recommendVC.nextPage];
//        
//    }];
//    
//}
//
//取消section的吸附效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.myTableView)
    {
        CGFloat sectionHeaderHeight = 60; //sectionHeaderHeight
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ModulesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cheapReuse"];
        cell.array = self.cheapArray;
        [cell.myCollectionView reloadData];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    if (indexPath.section == 1) {
        ModulesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lastReuse"];
        cell.array = self.lastArray;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.myCollectionView reloadData];
        return cell;
    }
    if (indexPath.section == 2) {
        ModulesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bestReuse"];
        cell.array = self.bestArray;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.myCollectionView reloadData];
        return cell;
    }if (indexPath.section == 3) {
        ModulesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"playReuse"];
        cell.array = self.playArray;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.myCollectionView reloadData];
        return cell;
    }if (indexPath.section == 4) {
        HunterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hunterReuse"];
        cell.array = self.hunterArray;
       // cell.backgroundColor = [UIColor grayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.collectionV reloadData];
        
        return cell;
    }
    SpotsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"spotsReuse"];
    cell.array = self.spotsArray;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.conllectionV reloadData];

    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
         return @"超值特价";
    }
    if (section == 1) {
        return @"最新上线";
    }if (section == 2) {
        return @"本周最佳";
    }if (section == 3) {
        return @"目的地玩法";
    }if (section == 4) {
        return @"明星猎人";
    }
    return @"体验故事";
    
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section <= 4) {
        return 240;
    }
    if (indexPath.row == 5) {
        return 190;
    }
    return 360;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
